package com.example.administrator.shzs.ui;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.activity.BaseActivity;
import com.example.administrator.shzs.adapter.GiftOrdeAdapter;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.entity.GiftOrdeData.GiftOrdeListData;
import com.example.administrator.shzs.url.Contents;
import com.example.administrator.shzs.utils.ToastUtils;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 礼品订单
 */
public class GiftOrderActivity extends BaseActivity {

    @Bind(R.id.mXRecyclerView_gift_orde_activity)
    XRecyclerView mXrecyclerView;
    @Bind(R.id.tv_not_gift_order)
    TextView tvNotGiftOrder;


    private String userId;

    private int pageIndex = 1;

    private List<GiftOrdeListData> listDatas;
    private List<GiftOrdeListData> mMoreDatas;

    private GiftOrdeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gift_order);
        ButterKnife.bind(this);
        MyApplication.app.activityList.add(this);
        setViews();

        userId= MyApplication.app.getUserId();
        getGifeOrdeFormServer(1);
        setRefresh();
    }


    /**
     * 刷新
     */
    private void setRefresh() {

        mXrecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        getGifeOrdeFormServer(1);
                        mXrecyclerView.refreshComplete();
                    }
                },500);

            }

            @Override
            public void onLoadMore() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pageIndex++;
                        getGifeOrdeFormServer(pageIndex);
                        mXrecyclerView.loadMoreComplete();
                    }
                },500);
            }
        });

    }

    /**
     * 从服务器加载数据
     *
     * @param pageIndex
     */
    private void getGifeOrdeFormServer(final int pageIndex) {

        String url = Contents.USER_GIFT_ORDE_LISTING + pageIndex + "&userid=" + userId;

        RequestParams params = new RequestParams(url);

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {

                    JSONObject object = new JSONObject(result);
                    JSONArray array = object.getJSONArray("datalist");

                    if(pageIndex==1) {
                        listDatas = JSON.parseArray(array.toString(), GiftOrdeListData.class);
                        if (listDatas.size() == 0) {
                            mXrecyclerView.setVisibility(View.GONE);
                            tvNotGiftOrder.setVisibility(View.VISIBLE);
                            return;
                        }
                        setAdapter();
                    }else {
                        mMoreDatas=JSON.parseArray(array.toString(), GiftOrdeListData.class);
                        if(mMoreDatas.size() == 0){
                            ToastUtils.showMessageShort(GiftOrderActivity.this,"没有更多了哦");
                        }
                        listDatas.addAll(mMoreDatas);
                        mMoreDatas.clear();
                        adapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 适配器
     */
    private void setAdapter() {

        adapter = new GiftOrdeAdapter(this, listDatas);
        mXrecyclerView.setAdapter(adapter);

    }

    private void setViews() {

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrecyclerView.setLayoutManager(manager);
        mXrecyclerView.setRefreshing(true);

        mXrecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mXrecyclerView.setLoadingMoreProgressStyle(ProgressStyle.SquareSpin);
        //刷新箭头
        mXrecyclerView.setArrowImageView(R.mipmap.ic_pulltorefresh_arrow);
    }
}
